from PyQt5.QtWidgets import (
    QPushButton, QLineEdit, QMessageBox, QDialog, QFormLayout, QVBoxLayout, QLabel, QHBoxLayout, QFileDialog, QSlider
)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIntValidator


class SettingsDialog(QDialog):
    def __init__(self, config_manager):
        super().__init__()
        self.strategy_path_button = None
        self.strategy_path_input = None
        self.ai_thread_count_input = None
        self.data_thread_count_input = None
        self.api_key_input = None
        self.data_count_slider = None
        self.data_count_label = None
        self.setWindowTitle("系统设置")
        self.setGeometry(200, 200, 500, 350)  # 调整窗口宽度和高度

        self.config_manager = config_manager
        self.config = self.config_manager.load_config()

        self.init_ui()

    def init_ui(self):
        main_layout = QVBoxLayout()  # 主布局为垂直布局

        # 配置表单布局
        form_layout = QFormLayout()
        form_layout.setHorizontalSpacing(20)  # 设置控件之间的水平间距
        form_layout.setVerticalSpacing(15)  # 设置控件之间的垂直间距

        # API Key 输入框
        self.api_key_input = QLineEdit()
        self.api_key_input.setText(self.config.get("AI_ZP_ApiKey", ""))
        self.api_key_input.setMinimumWidth(350)  # 设置最小宽度
        api_key_label = QLabel("智谱AI API_Key:")
        api_key_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)  # 标签右对齐
        form_layout.addRow(api_key_label, self.api_key_input)

        # 批量生成数据的工作线程数量
        self.data_thread_count_input = QLineEdit()
        self.data_thread_count_input.setText(str(self.config.get("data_thread_count", 10)))
        self.data_thread_count_input.setValidator(QIntValidator(1, 100))  # 设置输入范围为1到100
        data_thread_label = QLabel("数据生成线程数量:")
        data_thread_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        form_layout.addRow(data_thread_label, self.data_thread_count_input)

        # 批量调用 AI API 的线程数量
        self.ai_thread_count_input = QLineEdit()
        self.ai_thread_count_input.setText(str(self.config.get("ai_thread_count", 5)))
        self.ai_thread_count_input.setValidator(QIntValidator(1, 50))  # 设置输入范围为1到50
        ai_thread_label = QLabel("AI API 调用线程数量:")
        ai_thread_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        form_layout.addRow(ai_thread_label, self.ai_thread_count_input)

        # 保存生成策略文件的路径
        self.strategy_path_input = QLineEdit()
        self.strategy_path_input.setText(self.config.get("strategy_save_path", "./"))
        self.strategy_path_input.setMinimumWidth(350)
        self.strategy_path_button = QPushButton("选择路径")
        self.strategy_path_button.clicked.connect(self.choose_strategy_path)
        strategy_path_layout = QHBoxLayout()
        strategy_path_layout.addWidget(self.strategy_path_input)
        strategy_path_layout.addWidget(self.strategy_path_button)
        strategy_path_label = QLabel("生成策略保存路径:")
        strategy_path_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        form_layout.addRow(strategy_path_label, strategy_path_layout)

        # 测试数据条数中位数
        self.data_count_slider = QSlider(Qt.Horizontal)
        self.data_count_slider.setRange(100, 10000)
        self.data_count_slider.setValue(self.config.get("data_count_median", 500))
        self.data_count_slider.setTickInterval(100)
        self.data_count_slider.setTickPosition(QSlider.TicksBelow)
        self.data_count_slider.valueChanged.connect(self.update_slider_label)

        self.data_count_label = QLabel(f"{self.data_count_slider.value()}")  # 显示当前滑块值
        self.data_count_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)

        data_count_layout = QHBoxLayout()
        data_count_layout.addWidget(self.data_count_slider)
        data_count_layout.addWidget(self.data_count_label)

        data_count_label = QLabel("测试数据条数中位数:")
        data_count_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        form_layout.addRow(data_count_label, data_count_layout)

        main_layout.addLayout(form_layout)  # 将表单布局添加到主布局

        # 按钮布局
        button_layout = QHBoxLayout()
        save_button = QPushButton("保存")
        save_button.setFixedWidth(100)  # 设置按钮固定宽度
        save_button.clicked.connect(self.save_settings)

        button_layout.addStretch()  # 添加弹性空间
        button_layout.addWidget(save_button)  # 将按钮放在中间
        button_layout.addStretch()  # 添加弹性空间

        main_layout.addLayout(button_layout)  # 将按钮布局添加到主布局

        self.setLayout(main_layout)

    def update_slider_label(self):
        """更新滑块值的标签"""
        self.data_count_label.setText(str(self.data_count_slider.value()))

    def choose_strategy_path(self):
        """选择保存策略的路径"""
        directory = QFileDialog.getExistingDirectory(self, "选择保存路径", "./")
        if directory:
            self.strategy_path_input.setText(directory)

    def save_settings(self):
        """保存设置"""
        api_key = self.api_key_input.text().strip()
        data_thread_count = int(self.data_thread_count_input.text().strip())
        ai_thread_count = int(self.ai_thread_count_input.text().strip())
        strategy_save_path = self.strategy_path_input.text().strip()
        data_count_median = self.data_count_slider.value()

        # 更新配置
        self.config["AI_ZP_ApiKey"] = api_key
        self.config["data_thread_count"] = data_thread_count
        self.config["ai_thread_count"] = ai_thread_count
        self.config["strategy_save_path"] = strategy_save_path
        self.config["data_count_median"] = data_count_median

        self.config_manager.save_config(self.config)
        QMessageBox.information(self, "成功", "设置已保存！")
        self.accept()
